package com.gxa.admin.recycling.controller;

import com.github.pagehelper.PageInfo;
import com.gxa.admin.recycling.service.CommunityManageService;
import com.gxa.core.recycling.common.JsonResult;
import com.gxa.core.recycling.pojo.CommunityManage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 周志远
 */
@CrossOrigin
@RestController
@RequestMapping("/communityManage")
@Transactional(rollbackFor = {Throwable.class})
@Api(tags = "小区管理页面接口")
public class CommunityManageController {
    @Autowired
    CommunityManageService communityManageService;
    @PostMapping("/findByPage")
    @ApiOperation("查询所有小区管理+分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "查询的小区名称",required = false,dataType = "String"),
            @ApiImplicitParam(name = "provinces",value = "省市区",required = false,dataType = "String"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "每页显示数",required = true,dataType = "Integer")
    })
    public Map findByPage(String name,String provinces, Integer page, Integer size) throws IllegalAccessException {
        CommunityManage communityManage = new CommunityManage();
        communityManage.setName(name);
        communityManage.setProvinces(provinces);
        PageInfo<CommunityManage> communityManagePageInfo = communityManageService.selectByPage(page, size, communityManage);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(communityManagePageInfo.getList());
        jsonResult.put("count",communityManagePageInfo.getTotal());
        return jsonResult.getDataMap();
    }
    @PostMapping("/add")
    @ApiOperation("新增小区管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name",value = "查询的小区名称",required = false,dataType = "String"),
            @ApiImplicitParam(name = "provinces",value = "省市区",required = false,dataType = "String"),
            @ApiImplicitParam(name = "state",value = "审核状态",required = false,dataType = "Boolean"),
            @ApiImplicitParam(name = "remarks",value = "备注",required = false,dataType = "String")
    })
    public Map addCommunity(String name, String provinces,Boolean state,String remarks){
        CommunityManage communityManage = new CommunityManage();
        communityManage.setName(name);
        communityManage.setProvinces(provinces);
        communityManage.setState(state);
        communityManage.setRemarks(remarks);
        communityManage.setCreateTime(new Date());
        CommunityManage insert = communityManageService.insert(communityManage);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setMsg("添加成功");
        return jsonResult.getDataMap();
    }
    @PostMapping("/detail")
    @ApiOperation("查询单个小区管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "小区信息的id",required = true,dataType = "Long")

    })
    public Map detail(Long id){
        CommunityManage communityManage = communityManageService.selectById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(communityManage);
        return jsonResult.getDataMap();
    }
    @PostMapping("/update")
    @ApiOperation("编辑单个小区管理")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id",value = "小区信息的id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "name",value = "查询的小区名称",required = false,dataType = "String"),
            @ApiImplicitParam(name = "provinces",value = "省市区",required = false,dataType = "String"),
            @ApiImplicitParam(name = "remarks",value = "备注",required = false,dataType = "String")
    })
    public Map update(Long id,String name, String provinces,String remarks){
        CommunityManage communityManage = new CommunityManage();
        communityManage.setId(id);
        communityManage.setName(name);
        communityManage.setProvinces(provinces);
        communityManage.setRemarks(remarks);
        Integer integer = communityManageService.updateById(communityManage);
        JsonResult jsonResult = new JsonResult();
        if (integer > 0){
            jsonResult.setCode("200");
            jsonResult.setData(communityManage);
        }
        return jsonResult.getDataMap();
    }
    @PostMapping("/distribution")
    @ApiOperation("查询小区分配表+分页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "proviences",value = "省市区",required = false,dataType = "String"),
            @ApiImplicitParam(name = "name",value = "小区名称",required = false,dataType = "String"),
            @ApiImplicitParam(name = "isDistribution",value = "是否分配",required = false,dataType = "Boolean"),
            @ApiImplicitParam(name = "page",value = "页码",required = true,dataType = "Integer"),
            @ApiImplicitParam(name = "size",value = "每页显示数",required = true,dataType = "Integer")
    })
    public Map distribution(String proviences, String name, Boolean isDistribution, Integer page, Integer size){
        List<Map> maps = communityManageService.distributionCommunity(proviences, name, isDistribution, page, size);
        List<Map> maps1 = communityManageService.distributionCommunity(proviences, name, isDistribution, 1, 10000);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        jsonResult.put("count",maps1.size());
        return jsonResult.getDataMap();
    }
    @PostMapping("/distributionById")
    public Map findDistributionById(Long id){
        Map map = communityManageService.distributionById(id);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(map);
        return jsonResult.getDataMap();
    }
    @PostMapping("/recyclerList")
    @ApiOperation("所有骑手列表")
    public Map RecyclerList(){
        List<Map> maps = communityManageService.recyclerList();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(maps);
        return jsonResult.getDataMap();
    }
    @PostMapping("/deleteDistribution")
    @ApiOperation("删除小区分配")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "communityId",value = "小区id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "recyclerId",value = "骑手id",required = true,dataType = "Long")
    })
    public Map deleteDistribution(Long communityId,Long recyclerId){
        communityManageService.deleteDistribution(communityId, recyclerId);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }
    @PostMapping("/getAllCommunity")
    @ApiOperation("所有小区列表")
    public Map getAllCommunity(){
        List<CommunityManage> communityManages = communityManageService.selectAll();
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(communityManages);
        return jsonResult.getDataMap();
    }
    @PostMapping("/addDistributionCommunity")
    @ApiOperation("新增小区分配")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "communityId",value = "小区id",required = true,dataType = "Long"),
            @ApiImplicitParam(name = "recyclerId",value = "骑手id",required = true,dataType = "Long")
    })
    public Map addDistributionCommunity(Long communityId,Long recyclerId){
        communityManageService.addDistributionCommunity(communityId, recyclerId);
        CommunityManage communityManage = new CommunityManage();
        communityManage.setId(communityId);
        communityManage.setAllocateTime(new Date());
        communityManageService.updateById(communityManage);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        return jsonResult.getDataMap();
    }

    @PostMapping("/findCommunityManageByProvinces")
    @ApiOperation("根据省市区查询所有小区")
    public Map findCommunityManageByProvinces(@RequestParam("provices") String provinces){
        List<CommunityManage> allProvinces = communityManageService.findCommunityManageByProvinces(provinces);
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode("200");
        jsonResult.setData(allProvinces);
        return jsonResult.getDataMap();
    }
}
